import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { Md5 } from "ts-md5/dist/md5";
import { NavParams } from 'ionic-angular/navigation/nav-params';


/**
 * Generated class for the ForgetPwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-forget-pwd',
	templateUrl: 'forget-pwd.html',
})
export class ForgetPwdPage {

	

	phone_name: any;
	new_pwd: string;
	confirm_pwd: string;
	sendStatus:any;

	/*模型设置*/
	public findInfo = {
		mobile: '',
		veritycode: '',
	};

	//短信倒计时
	date = 0;
	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public viewCtrl: ViewController,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
			this.sendStatus = navParams.get('sendStatus');
		 }

	ionViewDidLoad() {

		console.log('ionViewDidLoad ForgetPwdPage');
	}

	/**
	* 找回密码（未登陆时）
	*/
	saveChange() {
		var subscriber = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
		var silly = subscriber.test(this.findInfo.mobile);

		if (this.findInfo.mobile == '' || this.findInfo.mobile == null) {
			this.dialog.showAlert('请输入手机号码', '');
			return;
		} if (this.findInfo.mobile.length != 11) {
			this.dialog.showAlert('请输入11位手机号码', '');
			return;
		}
		if (!silly) {
			this.dialog.showAlert('手机号不合法', '');
			return;
		} if (this.findInfo.veritycode == '' || this.findInfo.veritycode == null) {
			this.dialog.showAlert('请输入验证码', '');
			return;
		}
		if (this.findInfo.veritycode.length < 4 || this.findInfo.veritycode.length > 6) {
			this.dialog.showAlert('验证码是4-6位', '');
			return;
		}
		if (this.new_pwd == '' || this.new_pwd == null) {
			this.dialog.showAlert('请输入密码', '');
			return;
		}
		if (this.confirm_pwd == '' || this.confirm_pwd == null) {
			this.dialog.showAlert('请输入确认密码', '');
			return;
		}
		if (this.new_pwd.length < 6 || this.new_pwd.length > 16 || this.confirm_pwd.length < 6 || this.confirm_pwd.length > 16) {
			this.dialog.showAlert('密码必须6-16位', '');
			return;
		}
		// else if(this.new_pwd.length!=6||this.confirm_pwd.length!=6){
		// 	this.dialog.showAlert('警告','密码只能6位');
		// 	return;
		// }
		if (this.new_pwd == this.confirm_pwd) {
			var params = {
				phone: this.findInfo.mobile,
				newpassword: this.new_pwd,
				code: this.findInfo.veritycode
			};
			this.http.postJson('', 'users/member/retrievepassword', {
				'phone': params.phone,
				// 'password': Md5.hashStr(params.newpassword).toString(),md5加密格式
				"password": params.newpassword,
				"vcode": params.code
			}, '')
				.then(res => {
					if (res['status'] == 200) {
						this.dialog.showAlert(res['message'], '');
						//				this.navCtrl.push('LoginPage');
						this.navCtrl.pop();
					} else {
						this.dialog.showAlert(res['message'], '');

					}
				});
		} else {
			this.dialog.showAlert('两次输入的密码不一致', '');
		}

	}

	getSMS() {
		var subscriber = /^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8}$/;
		var silly = subscriber.test(this.findInfo.mobile);
		if (this.findInfo.mobile == '' || this.findInfo.mobile == null) {
			this.dialog.showAlert('请输入手机号码', '');
			return;
		} 
		if (this.findInfo.mobile.length != 11) {
			this.dialog.showAlert('请输入11位手机号码', '');
			return;
		}
		if (!silly) {
			this.dialog.showAlert('手机号不合法', '');
			return;
		}
		this.http.postJson('', 'users/member/sms',
			{
				mobiles: this.findInfo.mobile,
				type:"forget_pwd",
				sendStatus:this.sendStatus,
				send_from: 2
			}, '')
			.then(res => {
				let form = this;
				if (res['status'] === 200) {
					this.date = 60;
					const interval = setInterval(function () {
						form.date--;
						if (form.date === 0) { clearInterval(interval); }
					}, 1000);

				}else if(res['status'] === 500){
					this.dialog.showAlert(res['message'],'');
				}
			});
	}



}
